package bot;

import java.util.ArrayList;
import java.util.Collections;

import badugi.Card;
import badugi.Tools;

/**
 * Klasa implementujaca strategie gracza komputerowego na poziomie poczatkujacym
 * @authors Kornel Mirkowski, Maciej Krawczy
 */
public class Begginer implements Strategy {

	/**
	 * Metoda wymiany kart dla BOT'a
	 */
	@Override
	public String exchange(String x) {
		ArrayList<Card> cards = new ArrayList<Card>();
		cards.add(new Card(x.substring(0, 2)));cards.add(new Card(x.substring(2, 4)));
		cards.add(new Card(x.substring(4, 6)));cards.add(new Card(x.substring(6, 8)));

		ArrayList<Card> toEx = new ArrayList<Card>();
	
		cards.sort(null);
		if (cards.get(0).sameRankAs(cards.get(1)) && cards.get(0).sameRankAs(cards.get(2)) && cards.get(0).sameRankAs(cards.get(3))) {
			cards.remove(1);cards.remove(1);cards.remove(1);
		} else if (cards.get(0).sameRankAs(cards.get(1)) && cards.get(0).sameRankAs(cards.get(2))) {
			if (cards.get(0).sameSuitAs(cards.get(3))) {
				cards.remove(0); cards.remove(0);
			} else {
				cards.remove(1); cards.remove(1);
			}
		} else if (cards.get(1).sameRankAs(cards.get(2)) && cards.get(1).sameRankAs(cards.get(3))) {
			if (cards.get(1).sameSuitAs(cards.get(0))) {
				cards.remove(1); cards.remove(1);
			} else {
				cards.remove(2); cards.remove(2);
			}
		} else {
			boolean rotated = false;
			boolean finalRotated = false;
			int n=3;
			for (int i=0; i < n; i++) {
				for (int j=i+1; j <= n; j++) {
					if (cards.get(i).sameSuitAs(cards.get(j)) && !(cards.get(i).sameRankAs(cards.get(j)))) {
						toEx.add(cards.get(j));
						cards.remove(j);
						n--;
						j--;
					} else if ((finalRotated || rotated) && i != 1 && cards.get(i).eliminates(cards.get(j))) {
						toEx.add(cards.get(j));
						cards.remove(j);
						n--;
						j--;
					} else if (cards.get(i).sameRankAs(cards.get(j)) && !finalRotated) {
						if (i == n-1 && j == n) {
							toEx.add(cards.get(j));
							cards.remove(j);
							n--;
						} else if (cards.size() > j+1 && cards.get(j+1).sameRankAs(cards.get(j))) {
							Collections.rotate(cards, 1);
							finalRotated = true;
						} else if (cards.size() > i+2) {
							Collections.rotate(cards.subList(i, i+3), 1);
							rotated = true;
						}
						j--;
					}
				}
			}
		}

		String res = Tools.cardsToString(toEx);
		if (res.length() == 6 && cards.get(0).getValue() > 9)
			return x;
		else
			return res;
	}

	/**
	 * Metoda licytacji dla BOT'a
	 */
	@Override
	public String bid(String cards, int chips, int stake, int maxStake, boolean bet) {
		
		String situation = exchange(cards);
		int increase = (int) (maxStake * 10/100);
		
		if(situation.length() == 8)
			return "F";
		else if ((situation.length() == 0) && (increase + maxStake <= chips + stake)) {
			
			if(bet) {
				return "B" + increase;
			} else
				return "R" + increase;		
			
		} else if(stake == maxStake) {
			return "C";
		} else if(chips + stake > maxStake) {
			return "K";
		} else if(chips + stake <= maxStake) {
			return "A";
		} else {	
			System.out.println("Czyzby poprawa taktyki?");
			return "F";
		}
	}

}
